package com.cs.disposition.api;


import com.cs.disposition.api.common.AjaxResult;
import com.cs.disposition.entity.Disposition;
import com.cs.disposition.mqtt.MqttService;
import com.cs.disposition.service.DispositionService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/v1/devices")
@Slf4j
public class WxAPI {

    @Resource
    MqttService mqttService;

    @Resource
    DispositionService dispositionService;

    @PostMapping("/saveAndUpdateDisposition")
    public AjaxResult saveAndUpdateDisposition(String dispositionJsonStr){
        Disposition resDisposition = dispositionService.saveAndUpdateDisposition(dispositionJsonStr);
        if(resDisposition == null){
            return AjaxResult.error();
        }else {
            return AjaxResult.success();
        }
    }

    //=================================================
    @GetMapping("/publish")
    public AjaxResult publish() {
        try {
            mqttService.publish("/test", "ooooooooooooooooo");
        } catch (MqttException e) {
            throw new RuntimeException(e);
        }
        return AjaxResult.success();
    }

    @GetMapping("/getDisposition")
    public AjaxResult getDisposition(Long id) {
        return AjaxResult.success(dispositionService.findDispositionById(id));
    }
}
